package HTTPSendIntegrator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class SendReq extends MmsMessage {
	public StringBuffer sendconfMessage;
		
	public SendReq(){
		this.mmsMessageType = MMS_MESSAGE_TYPE_SEND_REQUEST;		
		this.mmsTo = new ArrayList<String>(); 
		this.mmsCC = new ArrayList<String>(); 
		this.mmsBCC = new ArrayList<String>();
		this.mmsParts = new ArrayList<MmsPart>();
	}
	
	public String toString(){
		String mmsMessage = "";
		
		if (!isMessageTypeSet()) return "MessageType not defined";
		if (!isTransactionIDSet()) return "transactionID not defined";
		if (!isVersionSet()) return "Version not defined";
		if (!isMessageContentTypeSet()) return "Content type not defined";		
		
		mmsMessage += MMS_MESSAGE_TYPE + " " + getMessageType() + NL;
		mmsMessage += MMS_TRANSACTION_ID + " " + getTransactionID() + NL;
		mmsMessage += MMS_VERSION + " " + getVersion() + NL;
		
		if (isMessageIDSet()) mmsMessage += MMS_MESSAGE_ID + " " + getMessageID() + NL;		
		if (isMessageDateSet()) mmsMessage += MMS_DATE + " " + getMessageDate() + NL; //.getTime()/1000 + NL;
		if (isMessageSenderSet()) mmsMessage += MMS_FROM + " " + getMessageSender() + NL;

		String list;
		if (isMessageReceiverSet()){
			Iterator<String> i = getMessageReceivers().iterator();	
			while(i.hasNext()){
				list = i.next();
				mmsMessage += MMS_TO + " " + list + NL;				
			}			
		}
		
		if (isMessageCCSet()){
			Iterator<String> i = getMessageCC().iterator();	
			while(i.hasNext()){
				list = i.next();
				mmsMessage += MMS_CC+ " " + list + NL;				
			}	
		}
		
		if (isMessageBCCSet()){
			Iterator<String> i = getMessageBCC().iterator();	
			while(i.hasNext()){
				list = i.next();
				mmsMessage += MMS_BCC + " " + list + NL;				
			}
		}

		if (isMessageSubjectSet()) mmsMessage += MMS_SUBJECT + " " + getMessageSubject() + NL;
		
		if (isDeliveryReportEnabled())
			if (isDeliveryReportEnabled()) mmsMessage += MMS_DELIVERY_REPORT + " " + MMS_DELIVERY_REPORT_YES + NL;
			else mmsMessage += MMS_DELIVERY_REPORT + " " + MMS_DELIVERY_REPORT_NO + NL;
			
		if (isReadReplyEnabled())		
			if (isReadReplyEnabled()) mmsMessage += MMS_READ_REPLY + " " + MMS_READ_REPLY_YES + NL;
			else mmsMessage += MMS_READ_REPLY + " " + MMS_READ_REPLY_NO + NL;

		if (isStoreEnabled())		
			if (isReadReplyEnabled()) mmsMessage += MMS_STORE + " " + MMS_STORE_YES + NL;
			else mmsMessage += MMS_STORE + " " + MMS_STORE_NO + NL;
		
		if (isMessageClassSet())		
		mmsMessage += MMS_CLASS + " " + getMessageClass() + NL;
		
		if (isMessageExpiryTimeSet())		 
			mmsMessage += MMS_EXPIRY + " " + getMessageExpiryTime() + NL;
		
		if (isMessageDeliveryTimeSet())
			mmsMessage += MMS_DELIVERY_TIME  + " " + getMessageDeliveryTime() + NL;
			
		/* ContentType MUST be the last header followed by the body */
		if (isMessageContentTypeSet()) mmsMessage += MMS_CONTENT_TYPE + " " + getMessageContentType() + NL;		
		
		MmsPart part;
		byte[]  partContent;
		Random random = new Random();
		long Randomboundary = random.nextLong();
		Iterator<MmsPart> j = mmsParts.iterator();
		if(j.hasNext()){
			part = j.next();
			mmsMessage += "type=" + part.getPartContentType() + NL;
			mmsMessage += "start=" + part.getPartId() + NL;
			mmsMessage += "boundary=" + Randomboundary + NL + NL;
		}
		
		Iterator<MmsPart> i = mmsParts.iterator();			
		while(i.hasNext()){
			part = i.next();
			mmsMessage += "--" + Randomboundary + NL;
			mmsMessage += "Content-ID: " + part.getPartId() + NL;
			mmsMessage += "Content-Type: " + part.getPartContentType() + NL;
			mmsMessage += "Content-Location: " + part.getPartContentType() + NL + NL;
			partContent = part.getPartContent();
			for (int x=0; x<partContent.length; x++) mmsMessage += partContent[x];
			mmsMessage += NL + NL;
		}
		
		return mmsMessage;
		
	}
}
